﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using OrbitOne.Elmah.Logic;

namespace OrbitOne.Elmah.Presenter
{
    public class ApplicationStatus : UserControl
    {
        #region Controls
        protected Repeater rptApplication;
        #endregion

        protected Color[] HealthStatusColors
        {
            get
            {
                return new[]
                           {
                                   Color.Green, 
                                   Color.Lime, 
                                   Color.LawnGreen, 
                                   Color.YellowGreen, 
                                   Color.Yellow,
                                   Color.Orange, 
                                   Color.OrangeRed,
                                   Color.Red
                           };
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            var service = new OrbitOneElmahErrorService();

            var days = service.GetApplicationsHealth(new TimeSpan(0, 24, 0, 0));
            var twoDay = service.GetApplicationsHealth(new TimeSpan(0, 48, 0, 0));
            var week = service.GetApplicationsHealth(new TimeSpan(7, 0, 0, 0));

            var applications = service.GetApplicationsHealth(new TimeSpan(31, 0, 0, 0))
                .Select(m => new ApplicationView
                                 {
                                     Name = m.Key,
                                     HealthMonth = m.Value,
                                     Health24 = days.SingleOrDefault(d => d.Key == m.Key).Value,
                                     Health48 = twoDay.SingleOrDefault(d => d.Key == m.Key).Value,
                                     HealthWeek = week.SingleOrDefault(d => d.Key == m.Key).Value
                                 });

            rptApplication.DataSource = applications.OrderByDescending(a => a.Health24)
                                                    .ThenByDescending(a => a.Health48)
                                                    .ThenByDescending(a => a.HealthWeek)
                                                    .ThenByDescending(a => a.HealthMonth);
            rptApplication.DataBind();
        }

        protected string GetStatusColor(double status)
        {
            if(status == 0) return null;

            var index = (int)status;
            int result;
            if (index < HealthStatusColors.Length)
            {
                result = index;
            }
            else
            {
                result = HealthStatusColors.Length - 1;
            }
            return HealthStatusColors[result].Name;
        }
    }
    public class ApplicationView
    {
        public string Name { get; set; }
        public double Health24 { get; set; }
        public double Health48 { get; set; }
        public double HealthWeek { get; set; }
        public double HealthMonth { get; set; }
        public double HealthYear { get; set; }
    }
}
